//
//  BSCoinCell.m
//  BDSprite
//
//  Created by alibaba on 2017/9/1.
//  Copyright © 2017年 ESTGroup. All rights reserved.
//

#import "BSCoinCell.h"
#import "BaseView.h"

#import <Masonry/Masonry.h>
#import "UserProfileViewController.h"

static CGFloat topicListCellAvatarHeight = 18;

@interface BSCoinCell ()
@property (nonatomic, strong) UIImageView *avatarImageView;
@property (nonatomic, strong) UILabel *topicTitleLabel;

@property (nonatomic, strong) UILabel *currentPriceLabel;
@property (nonatomic, strong) UILabel *percentLabel;
@property (nonatomic, strong) UISwitch *focuSwitch;

@property (nonatomic, assign) BOOL didSetupConstraints;
@end

@implementation BSCoinCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUI];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setUI
{
    [self.contentView addSubview:self.avatarImageView];
    [self.contentView addSubview:self.topicTitleLabel];
    [self.contentView addSubview:self.currentPriceLabel];
    [self.contentView addSubview:self.percentLabel];
    
    [self.contentView addSubview:self.focuSwitch];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (void)setCoinInfo:(BSCoinModel *)coinInfo
{
    _coinInfo = coinInfo;
    [_avatarImageView setImageURL:[NSURL URLWithString:_coinInfo.infoBean.imageurl]];
    _topicTitleLabel.text = [NSString stringWithFormat:@"%@ - %@",_coinInfo.chinesename,_coinInfo.englishname];
    _currentPriceLabel.text = [NSString stringWithFormat:@"价格:%@",_coinInfo.price];
    _percentLabel.text = _coinInfo.percentStr;
    
    if (_coinInfo.percent > 0) {
        _percentLabel.textColor = [UIColor redColor];
    }else{
        _percentLabel.textColor = [UIColor greenColor];
    }
}

- (void)focusBites:(UISwitch *)sender
{
    if(sender.on){
        [SVProgressHUD showInfoWithStatus:@"关注成功"];
    }else
    {
        [SVProgressHUD showInfoWithStatus:@"取消关注"];
    }
}
#pragma mark- set get

- (UIImageView *)avatarImageView {
    if (!_avatarImageView) {
        _avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, kIPhone6PScale(20), kIPhone6PScale(20))];
        _avatarImageView.userInteractionEnabled = YES;
        _avatarImageView.image = [UIImage imageNamed:@"twitter_icon"];
    }
    return _avatarImageView;
}

- (UILabel *)topicTitleLabel {
    if (!_topicTitleLabel) {
        _topicTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_avatarImageView.right+10, kIPhone6PScale(2), kIPhone6PScale(140), kIPhone6PScale(39))];
        _topicTitleLabel.font = [UIFont fontWithName:FontName size:14];
        _topicTitleLabel.numberOfLines = 2;
    }
    return _topicTitleLabel;
}

- (UILabel *)currentPriceLabel
{
    if (!_currentPriceLabel) {
        _currentPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(_topicTitleLabel.right+10, 55, kIPhone6PScale(70), 15)];
        _currentPriceLabel.font = [UIFont fontWithName:BoldFontName size:12];
        _currentPriceLabel.adjustsFontSizeToFitWidth = YES;
//        _currentPriceLabel.backgroundColor = [UIColor redColor];
    }
    return _currentPriceLabel;
}

- (UILabel *)percentLabel
{
    if (!_percentLabel) {
        _percentLabel = [[UILabel alloc] initWithFrame:CGRectMake(_currentPriceLabel.right, 55, kIPhone6PScale(70), 15)];
        _percentLabel.font = [UIFont fontWithName:BoldFontName size:12];
        _percentLabel.adjustsFontSizeToFitWidth = YES;
    }
    return _percentLabel;
}

- (UISwitch *)focuSwitch
{
    if (!_focuSwitch) {
        _focuSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 60, 2, 33, 21)];
        _focuSwitch.on = NO;
        _focuSwitch.userInteractionEnabled = YES;
        [_focuSwitch addTarget:self action:@selector(focusBites:) forControlEvents:UIControlEventValueChanged];
    }
    return _focuSwitch;
}

#pragma mark Tap User Avatar

- (void)didTapAvatarImageView {
    UserProfileViewController *userProfileVC = [[UIStoryboard storyboardWithName:@"UserProfile"
                                                                          bundle:[NSBundle mainBundle]]
                                                instantiateViewControllerWithIdentifier:@"userprofile"];
    [JumpToOtherVCHandler pushToOtherView:userProfileVC animated:YES];
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ([[gestureRecognizer view] isKindOfClass:[UITableViewCell class]]) {
        return NO;
    }
    return YES;
}

@end
